package dm71.espil;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.content.Context;
import java.util.List;

/**
 * TODO Put here a description of what this class does.
 * 
 * @author Moulde. Created Jan 6, 2012.
 */
public class AccelerometerManager
{
	static SensorManager sensorManager;
	static boolean running;
	static Boolean isSupported;

	static float threshold = 0.2f;
	static int interval = 1000;

	static Sensor sensor;
	static AccelerometerListener listener;

	static Activity context;

	public static void SetContext(Activity context)
	{
		AccelerometerManager.context = context;
	}

	public static void StartListening(AccelerometerListener listener)
	{
		sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);

		List<Sensor> sensors = sensorManager
				.getSensorList(Sensor.TYPE_ACCELEROMETER);
		if (sensors.size() > 0)
		{
			sensor = sensors.get(0);
			running = sensorManager.registerListener(sensorEventListener,
					sensor, SensorManager.SENSOR_DELAY_GAME);
			AccelerometerManager.listener = listener;
		}
	}

	public static boolean IsSupported()
	{
		if (isSupported == null)
		{
			if (context != null)
			{
				sensorManager = (SensorManager) context
						.getSystemService(Context.SENSOR_SERVICE);
				List<Sensor> sensors = sensorManager
						.getSensorList(Sensor.TYPE_ACCELEROMETER);
				isSupported = new Boolean(sensors.size() > 0);
			} else
			{
				isSupported = Boolean.FALSE;
			}
		}
		return isSupported;
	}

	public static void StartListening(AccelerometerListener listener,
			float threshold, int interval)
	{
		Configure(threshold, interval);
		StartListening(listener);
	}

	public static void stopListening()
	{
		running = false;
		try
		{
			if (sensorManager != null && sensorEventListener != null)
			{
				sensorManager.unregisterListener(sensorEventListener);
			}
		} catch (Exception e)
		{
		}
	}

	public static void Configure(float threshold, int interval)
	{
		AccelerometerManager.threshold = threshold;
		AccelerometerManager.interval = interval;
	}

	private static SensorEventListener sensorEventListener = new SensorEventListener() {
		long now, timeDifference, lastUpdate, lastShake = 0;

		float x, y, z = 0;
		float lastX, lastY, lastZ = 0;
		float force = 0;

		public void onSensorChanged(SensorEvent event)
		{
			now = event.timestamp;

			x = event.values[0];
			y = event.values[1];
			z = event.values[2];

			if (lastUpdate == 0)
			{
				lastUpdate = now;
				lastShake = now;

				lastX = x;
				lastY = y;
				lastZ = z;
			}
			else
			{
				timeDifference = now - lastUpdate;
				if (timeDifference > 0)
				{
					force = (Math.abs(x + y + z - lastX - lastY - lastZ) / timeDifference) * 1000000;
					if (force > threshold)
					{
						if (now - lastShake >= interval)
						{
							listener.onShake(force);
						}
						
						lastShake = now;
					}
					lastX = x;
					lastY = y;
					lastZ = z;
					lastUpdate = now;
				}
			}

			listener.onAccelerationChanged(x, y, x);
		}

		public void onAccuracyChanged(Sensor sensor, int accuracy)
		{
		}
	};
}
